import sys
import base64

import cs_api


if len(sys.argv) != 4:
    print "deploy_vm.py name userdata_file ec2_config"
    sys.exit(1)

cs_api._EC2_CONFIG = sys.argv[3]

mCVM_service_offering_id = cs_api.get_service_offering_id('m1.medium')

mCVM_template_id = cs_api.get_template_id('ucvm')

mCVM_zone_id = cs_api.get_zone_id('zone0')

mCVM_disk_offering_id = cs_api.get_disk_offering_id('Medium')

mCVM_display_name = sys.argv[1]

mCVM_network_id = cs_api.get_network_id('defaultGuestNetwork')

mCVM_security_group_id = cs_api.get_security_group_id('default')

userdata = open(sys.argv[2]).read()

userdata64 = base64.b64encode(userdata)

name = mCVM_display_name

vm = cs_api.command(cs_api.EC2Command(command='deployVirtualMachine',
                                        serviceofferingid=mCVM_service_offering_id,
                                        templateid=mCVM_template_id,
                                        zoneid=mCVM_zone_id,
                                        name=name,
                                        diskofferingid=mCVM_disk_offering_id,
                                        displayname=mCVM_display_name,
#                                        networkids=mCVM_networkid,
                                        securitygroupid=mCVM_security_group_id,
#                                        ipaddress='10.1.2.27',
                                        userdata=userdata64
                                        ),
                     timeout=50)

print vm

